﻿using System;
using System.Globalization;
using System.Threading;
using System.Web.Mvc;
using System.Web.Routing;
using EnetaMvc.Registries;
using log4net;
using StructureMap;

namespace EnetaMvc.Web
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Login",
                "login",
                new { controller = "Home", action = "Login" }
            );
            routes.MapRoute(
                "LogOut",
                "logout",
                new { controller = "Home", action = "LogOut" }
            );
            routes.MapRoute(
                "Register",
                "register",
                new { controller = "Home", action = "Register" }
            );

            routes.MapRoute(
                "Tooltip", // Route name
                "tooltip/{tipName}", // URL with parameters
                new { controller = "Tooltip", action = "Index", tipName = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
                "Articles", // Route name
                "news/articles/{Action}/{id}", // URL with parameters
                new { controller = "Articles", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
                "BlogPostsPaging", // Route name
                "news/blogs/posts/page/{page}", // URL with parameters
                new { controller = "BlogPosts", action = "Index" } // Parameter defaults
            );

            routes.MapRoute(
                "BlogPosts", // Route name
                "news/blogs/posts/{Action}/{id}", // URL with parameters
                new { controller = "BlogPosts", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
                "BlogPostsByBlogPaging", // Route name
                "news/blogs/details/{id}/page/{page}", // URL with parameters
                new { controller = "Blogs", action = "Details", id = UrlParameter.Optional } // Parameter defaults
            );
            routes.MapRoute(
                "BlogPostsByBlog", // Route name
                "news/blogs/{Action}/{id}", // URL with parameters
                new { controller = "Blogs", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
            routes.MapRoute(
                "ForumPaged", // Route name
                "forums/forum/{id}/page/{page}", // URL with parameters
                new { controller = "Forums", action = "Forum", id = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
               "AdminTagGroups", // Route name
               "admin/TagGroups/{action}/{id}", // URL with parameters
               new { controller = "TagGroups", action = "Index", id = UrlParameter.Optional } // Parameter defaults
           );

            routes.MapRoute(
                "SlaLogs", // Route name
                "admin/SlaMonitoringLogs/Index/page/{page}", // URL with parameters
                new { controller = "SlaMonitoringLogs", action = "Index" } // Parameter defaults
            );

            routes.MapRoute(
                "SlaLogs2", // Route name
                "admin/SlaMonitoringLogs/{action}", // URL with parameters
                new { controller = "SlaMonitoringLogs", action = "Index" } // Parameter defaults
            );

            routes.MapRoute(
                "Paging", // Route name
                "{controller}/page/{page}", // URL with parameters
                new { action = "Index" } // Parameter defaults
            );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            log4net.Config.XmlConfigurator.Configure();
            
            ObjectFactory.Initialize(container =>
            {
                container.AddRegistry<CloudRegistry>();                
                container.For<ILog>().Use(LogManager.GetLogger("Log"));
            });
                       
            ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Response.Write(Server.GetLastError());
            Server.ClearError();
            Response.End();
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }
    }
}